package rs.mbs2.epics.server.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@SuppressWarnings("serial")
@Entity
@Table(name="rating")
@NamedQueries({				
	@NamedQuery(name= "findRatingByPhotoAndUser",
				query="SELECT r FROM Rating r WHERE r.userThatRated.id LIKE :userId and r.photo.id LIKE :photoId")
})
public class Rating implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "rating_id", unique = true, nullable = false)
	private Integer id;
	
	@Column(name = "rating_vote", unique = false, nullable = false)
	private Integer vote;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
	private User userThatRated;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "photo_id", referencedColumnName = "photo_id", nullable = false)
	private Photo photo;
	
	
	public Rating() {
		
	}

	public Rating(Integer vote, User userThatRated, Photo photo) {
		
		super();
		this.vote = vote;
		this.userThatRated = userThatRated;
		this.photo = photo;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getVote() {
		return vote;
	}

	public void setVote(Integer vote) {
		this.vote = vote;
	}

	public User getUserThatRated() {
		return userThatRated;
	}

	public void setUserThatRated(User userThatRated) {
		this.userThatRated = userThatRated;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((photo == null) ? 0 : photo.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Rating other = (Rating) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (photo == null) {
			if (other.photo != null)
				return false;
		} else if (!photo.equals(other.photo))
			return false;
		return true;
	}
	
}
